package com.admin.system.modules.system.rest;

import com.admin.common.constant.ExceptionConstant;
import com.admin.common.entity.BaseEntity;
import com.admin.common.exception.BadRequestException;
import com.admin.common.utils.PageResult;
import com.admin.common.utils.PageUtil;
import com.admin.component.web.MultipartFileUtil;
import com.admin.core.dto.DeptQueryDTO;
import com.admin.core.entity.SysDept;
import com.admin.logging.annotation.Log;
import com.admin.logging.enums.OperateTypeEnum;
import com.admin.system.modules.system.service.DeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author sq-lan
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "系统：部门管理")
@RequestMapping("/api/dept")
public class DeptController {

    private final DeptService deptService;

    @ApiOperation("查询")
    @GetMapping
    @PreAuthorize("@ss.check('user:list','dept:list')")
    public ResponseEntity<PageResult<SysDept>> list(DeptQueryDTO dto) {
        List<SysDept> list = deptService.list(dto);
        return new ResponseEntity<>(PageUtil.toPage(list), HttpStatus.OK);
    }

    @Log(module = "部门管理", operateType = OperateTypeEnum.ADD)
    @ApiOperation("新增")
    @PostMapping
    @PreAuthorize("@ss.check('dept:add')")
    public ResponseEntity<Object> add(@Validated @RequestBody SysDept entity) {
        if (entity.getId() != null) {
            throw new BadRequestException(ExceptionConstant.ERROR_PARAM);
        }
        deptService.save(entity);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log(module = "部门管理", operateType = OperateTypeEnum.MODIFY)
    @ApiOperation("修改")
    @PutMapping
    @PreAuthorize("@ss.check('dept:edit')")
    public ResponseEntity<Object> edit(@Validated(BaseEntity.Update.class) @RequestBody SysDept entity) {
        deptService.modify(entity);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log(module = "部门管理", operateType = OperateTypeEnum.REMOVE)
    @ApiOperation("删除")
    @DeleteMapping
    @PreAuthorize("@ss.check('dept:del')")
    public ResponseEntity<Object> remove(@RequestBody Set<Integer> ids) {
        deptService.remove(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("导出部门数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@ss.check('dept:list')")
    public void download(HttpServletResponse response, DeptQueryDTO dto) throws Exception {
        List<SysDept> deptList = deptService.list(dto);
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysDept dept : deptList) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("部门名称", dept.getName());
            map.put("部门状态", dept.getEnabled() ? "启用" : "停用");
            map.put("创建日期", dept.getCreateTime());
            list.add(map);
        }
        MultipartFileUtil.downloadExcel(list, response);
    }
}
